package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.sponsor.SponsorTransfer;
import com.admin.education.services.Interfaces.ISponsorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController(value = "adminSponsorController")
@RequestMapping("sponsors")
@CrossOrigin(origins = "*", maxAge = 3600)
public class SponsorController {
    @Autowired
    private ISponsorService sponsorService;
    @GetMapping("")
    public Callable<IResponseMessage> querySponsor(@Valid PageRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<SponsorTransfer>> result = sponsorService.querySponsorsForAudit(pageRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<SponsorTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{sponsorId}")
    public  Callable<IResponseMessage> querySponsorById(@PathVariable Long sponsorId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<SponsorTransfer> result = sponsorService.querySponsors(sponsorId);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<SponsorTransfer>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @DeleteMapping("{sponsorId}")
    public Callable<IResponseMessage> deleteSponsorById(@PathVariable Long sponsorId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = sponsorService.deleteSponsor(sponsorId);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PostMapping("{userInformationId}/Pass")
    public Callable<IResponseMessage> pass(@PathVariable Long userInformationId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = sponsorService.auditSponsor(userInformationId,true);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PostMapping("{userInformationId}/NoPass")
    public Callable<IResponseMessage> noPass(@PathVariable Long userInformationId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result =  sponsorService.auditSponsor(userInformationId,false);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
